#ifndef SYNCBASETH_H
#define SYNCBASETH_H
/********************************************************************************
** @ Date:2025/05/22
** @ Author: HanJiaXin
** @ Description:SyncBase
** @ Version:1.0.0
** @ ClassName:CClientCfgDlg
** @ Fun:
********************************************************************************/
#include <QThread>
#include <QMutex>
#include <QLibrary>
#include <QDateTime>
#include <QMap>
#include "cglobal.h"

class CClientData;
class CTagData;

class CSyncBaseTh : public QThread
{

public:
    CSyncBaseTh();//
    ~CSyncBaseTh();
    void threadStart();
    void threadStop();

private:
    GETMODULEPTREX m_lpGetModelPtr;
    void initModule();      //初始化驱动模块
    void startModule();
    void endModule();
    void writeTag();        //写实时测点
    void writeTagHisTory(); //写历史测点
    static void addMsg(const char* message, int nLevel);
    void addMsg(int nLevel, const QString& sMsg, QDateTime dtm = QDateTime::currentDateTime());

protected:
    void run() override;

private:
    bool m_bShouldStop;
    bool m_bFrist;                           //是否是首次开启线程，防止重启启动设备的时候频繁创建线程
    bool m_bTagChanged;                      //测点集是否变化
    int m_nTagChanged;                       //测点集是否发生变化
    qint64 m_nTagTimeOut;                       //测点状态超时时间 单位秒
    QMutex m_mutex;                       //资源锁
    std::atomic<bool>      m_bhread;         //线程状态
    QLibrary* m_pLib;
    CModuleParent* m_pModule;     //SyncBase驱动操作指针

    QMap<CClientData*, QVector<CTagData*>> m_mapClient;     //客户端与测点对照
};

#endif // SYNCBASETH_H
